# 读取文件
dataset <- read.table(
  file = "C:/Users/LoveP/Desktop/码云/R-Study/data/pima-indians-diabetes.data.csv",
  sep = ",", header=FALSE, na.strings=".")
# 随机分两个数据集
sample <- sample(nrow(dataset), nrow(dataset) * 0.67)
ds1 <- dataset[sample, ]
ds2 <- dataset[sample * -1, ]
# 分成两个类别
dat=split(ds1,ds1$V9)
dat0 <- rbind(lapply(dat$`0`,mean), lapply(dat$`0`,sd))[, -9]
dat1 <- rbind(lapply(dat$`1`,mean), lapply(dat$`1`,sd))[, -9]

i <- 1
sum <- 0
while(i<=nrow(ds2)){
  sum1 <- 1
  sum2 <- 1
  j <- 1
  while(j<=length(ds2[i,1:8])) {
    sum1 = sum1 * dnorm(as.numeric(ds2[i,][j]), as.numeric(dat0[1,j]), as.numeric(dat0[2,j]))
    sum2 = sum2 * dnorm(as.numeric(ds2[i,][j]), as.numeric(dat1[1,j]), as.numeric(dat1[2,j]))
    j <- j + 1
  }
  if((sum1 > sum2 & ds2[i,9] == "0") || (sum1 <= sum2 & ds2[i,9] == "1")){
    sum <- sum + 1
  }
  i <- i + 1
}
print(sum/nrow(ds2))